package com.derbysoft.ota.application.translator.rs;

import java.util.List;
import java.util.Map.Entry;

import com.derbysoft.modules.eip.translator.Translator;
import com.derbysoft.ota.interfaces.protocol._2009a.OTAHotelSearchRQ;
import com.derbysoft.ota.interfaces.protocol._2009a.PropertiesType;
import com.derbysoft.ota.interfaces.protocol._2009a.PropertiesType.Property;
import com.derbysoft.remote.dswitch.hotel.dto.HotelSearchPropertyDTO;
import com.derbysoft.remote.dswitch.hotel.dto.HotelSearchRS;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.googlecode.functionalcollections.FunctionalIterables;
import org.apache.commons.collections.CollectionUtils;

public class HotelSearchRSTranslator implements Translator<Entry<OTAHotelSearchRQ, HotelSearchRS>, PropertiesType>  {
	
	private HotelPropertyTranslator hotelPropertyTranslator;
	
	@Inject
    public HotelSearchRSTranslator(HotelPropertyTranslator hotelPropertyTranslator) {
		this.hotelPropertyTranslator = hotelPropertyTranslator;
	}

	@Override
	public PropertiesType translate(final Entry<OTAHotelSearchRQ, HotelSearchRS> source) {
        List<HotelSearchPropertyDTO> switchProperties = source.getValue().getHotelSearchProperties();
        if (CollectionUtils.isEmpty(switchProperties)) {
            return null;
        }
        PropertiesType propertiesType = new PropertiesType();
        propertiesType.getProperty().addAll(
        	FunctionalIterables.make(switchProperties).map(
        		new Function<HotelSearchPropertyDTO, Property>() {
					public Property apply(HotelSearchPropertyDTO from) {
						return hotelPropertyTranslator.translate(Maps.immutableEntry(source.getKey(), from));
					}
				}
        	).toCollection()
        );
        return propertiesType;
	}

}
